package com.diligent.cloud.sfc.factory.impl;

import com.alibaba.fastjson.JSON;
import com.canny.commons.model.Codes;
import com.canny.commons.model.ListResult;
import com.canny.commons.model.Result;
import com.diligent.cloud.logistics.container.service.LcmBoxLedgerFeignService;
import com.diligent.cloud.sfc.config.OauthConfig;
import com.diligent.cloud.sfc.factory.StationFactory;
import com.diligent.cloud.sfc.model.MatType;
import com.diligent.cloud.sfc.model.detail.PackagingDetailInfo;
import com.diligent.cloud.sfc.service.DpsService;
import com.diligent.cloud.sfc.service.ISfcService;
import com.diligent.cloud.sfc.service.WorkshopScenePackingService;
import com.diligent.cloud.sfc.service.impl.WipServiceImpl;
import com.diligent.cloud.sfc.util.ConverterUtil;
import com.diligent.cloud.wip.core.vo.StorageLedgerInfo;
import com.diligent.cloud.wip.panel.modle.WipStorageLedger;
import com.diligent.cloud.workorder.model.*;
import com.diligent.cloud.workorder.service.WoOrderFeignService;
import com.diligent.cloud.workorder.service.WoOrderMatNeedFeignService;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * PackagingStationStation class
 *
 * @author xf107278
 * @date 4/3/2020
 */
@Component
public class PackagingStation implements StationFactory {


    private LcmBoxLedgerFeignService lcmBoxLedgerFeignService;
    private final OauthConfig.RequestParams requestParams;
    private final ISfcService sfcService;
    private final DpsService dpsService;
    private final WoOrderMatNeedFeignService orderMatNeedFeignService;
    private final WearStripsStation wearStripsStation;
    private final WipServiceImpl wipServiceImpl;
    private final WoOrderFeignService orderFeignService;
    private final WorkshopScenePackingService workshopScenePackingService;



    public PackagingStation(LcmBoxLedgerFeignService lcmBoxLedgerFeignService,
                            OauthConfig.RequestParams requestParams, ISfcService sfcService, DpsService dpsService,
                            WoOrderMatNeedFeignService orderMatNeedFeignService, WearStripsStation wearStripsStation,
                            WipServiceImpl wipServiceImpl, WoOrderFeignService orderFeignService,
                            WorkshopScenePackingService workshopScenePackingService) {
        this.lcmBoxLedgerFeignService = lcmBoxLedgerFeignService;
        this.requestParams = requestParams;
        this.sfcService = sfcService;
        this.dpsService = dpsService;
        this.orderMatNeedFeignService = orderMatNeedFeignService;
        this.wearStripsStation = wearStripsStation;
        this.wipServiceImpl = wipServiceImpl;
        this.orderFeignService = orderFeignService;
        this.workshopScenePackingService = workshopScenePackingService;
    }

    /***
     * 根据同包装方式工单
     *  根据order的机台去查找dps 同类型工单
     *      得到未完成工单And完成支数<30
     *      根据需求数查找
     * @param order
     * @return
     */
    @Override
    public Result getOrderInfo(WoOrder order) {
        PackagingDetailInfo packagingDetailInfo = new PackagingDetailInfo();
        //查询到所有dps单

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//
//        WoOrderAndMatNeedParams woOrderAndMatNeedParams = new WoOrderAndMatNeedParams();
//        woOrderAndMatNeedParams.setMatType(sfcService.getMatType(MatType.包装物));
//        woOrderAndMatNeedParams.setProduceDate(simpleDateFormat.format(order.getProduceDate()));
//        woOrderAndMatNeedParams.setWoOrderType(order.getOrderType().toString());
//        //4 和 5？
//        woOrderAndMatNeedParams.setWoOrderStatus(order.getOrderStatus().toString());
//        woOrderAndMatNeedParams.setMachCode(order.getMachineCode());
//        woOrderAndMatNeedParams.setSystemId(order.getSystemId());
//        ResponseEntity<com.diligent.cloud.primary.data.model.Result> orderNedResource =
//                orderFeignService.selectWoOrderAndMatNeed(woOrderAndMatNeedParams);

        String matCode = sfcService.getMatCodeByOrderIdAndSystemId(order.getId(),
                order.getSystemId(), MatType.包装物);

        DpsListParams dpsListParams = new DpsListParams();
        dpsListParams.setPageSize(1L);
        dpsListParams.setPageIndex(1L);
        dpsListParams.setSystemId(order.getSystemId());
        dpsListParams.setProduceDate(simpleDateFormat.format(order.getProduceDate()));
        dpsListParams.setOrderType(order.getOrderType());
        dpsListParams.setMachineCode(order.getMachineCode());
        ResponseEntity<ListResult> dpsList = dpsService.selectDpsList(dpsListParams);
        dpsListParams.setPageSize(Long.valueOf(dpsList.getBody().getMsg()));
         dpsList = dpsService.selectDpsList(dpsListParams);
//
//
//        ArrayList<LinkedHashMap> orderMatNetList = (ArrayList<LinkedHashMap>) orderNedResource.getBody().getData();


        Integer samePackagingOrder = 0;
        Integer unfinishedOrder = 0;

        //成品同料
        Integer totalSameMatNeedDigit = 0;
        //当班成品需求数
        Integer onDutyfgNeedDigit = 0;
        //当班成品查询
        List<StorageLedgerInfo> fgStockList = new ArrayList<>();

        StorageLedgerInfo storageLedgerInfo;
        for (LinkedHashMap orderMap : (ArrayList<LinkedHashMap>)dpsList.getBody().getData()) {
            WoOrder woOrder = JSON.parseObject(JSON.toJSONString(orderMap), WoOrder.class);

            //同包装
            if (woOrder.getCv8().equalsIgnoreCase(order.getCv8())) {
                //完成度
//                WoOrderCompleteDetail orderCompleteById =
//                        sfcService.getOrderCompleteByOrder(woOrder.getId(), null);
//                if (orderCompleteById == null) {
//                    samePackagingOrder++;
//                } else if (orderCompleteById != null & orderCompleteById.getGoodDigit().intValue() < 30) {
//                    samePackagingOrder++;
//                    unfinishedOrder++;
                    samePackagingOrder++;
              if (woOrder.getDeliveryDigit().intValue()  < 30) {
                    unfinishedOrder++;
                }
            }
            //成品
            if (woOrder.getMatCode().equalsIgnoreCase(order.getMatCode())){
                totalSameMatNeedDigit += woOrder.getDeliveryDigit().intValue();
            }
            onDutyfgNeedDigit  += woOrder.getDeliveryDigit().intValue();
            storageLedgerInfo = new StorageLedgerInfo();
            storageLedgerInfo.setMatCode(woOrder.getMatCode());
            fgStockList.add(storageLedgerInfo);
        }
        //包材当班
        WipStorageLedger storageLedger;
        packagingDetailInfo.setPmStock(wearStripsStation.getOnDutySchedule(matCode, order));


        //同型号
        packagingDetailInfo.setSameMatPmStock(wearStripsStation.getSameMatSchedule(order.getCv8(), order));


        //成品当班
        storageLedger =
                (WipStorageLedger) (wipServiceImpl.getInventoryByMatCodeAndCwocCodes(order.getWorkCenterCode(),
                        order.getSystemId(),
                        fgStockList).getBody().getData());
        if (storageLedger.getDigit().longValue() != 0) {
            packagingDetailInfo.setFgStock(storageLedger.getDigit().longValue() / onDutyfgNeedDigit);
        } else {
            packagingDetailInfo.setFgStock(0L);
        }


        //成品同料
        storageLedger =
                (WipStorageLedger)
                        (wipServiceImpl.getInventoryByMatCodeAndCwocCode(order.getMatCode(),
                                order.getWorkCenterCode(), order.getBatchCode(), order.getSystemId())).getBody().getData();
        if (storageLedger.getDigit().longValue() != 0) {
            packagingDetailInfo.setSameMatFgStock(storageLedger.getDigit().longValue() / totalSameMatNeedDigit);
        } else {
            packagingDetailInfo.setSameMatFgStock(0L);
        }


        ListResult lotsByOrder = workshopScenePackingService.getLotsByOrder(order.getId());
        Integer total = 0;
        for (WoLots lots : (List<WoLots>) lotsByOrder.getData()) {
            total += lots.getDigit().intValue();
        }
        packagingDetailInfo.setCompleteDigit(total);
        packagingDetailInfo.setGoodDigit(total);
        packagingDetailInfo.setSamePackagingOrder(samePackagingOrder);
        packagingDetailInfo.setUnfinishedOrder(unfinishedOrder);
        packagingDetailInfo.setPlanDigit(order.getDeliveryDigit().intValue());
        packagingDetailInfo.setPackingDigit(order.getStandardDigit().intValue());


        return ConverterUtil.ConverterByData(packagingDetailInfo, Codes.success.getCode(),
                Codes.success.getMsg());
    }

}
